package com.ece.core.versioning;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Collection;
import java.util.Iterator;

import org.tmatesoft.svn.core.SVNCommitInfo;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNPropertyValue;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.util.SVNURLUtil;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDiffWindow;
import org.tmatesoft.svn.core.wc.ISVNOptions;
import org.tmatesoft.svn.core.wc.SVNCommitClient;
import org.tmatesoft.svn.core.wc.SVNDiffClient;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.tmatesoft.svn.core.wc.SVNUpdateClient;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

public class BasicVersioningOperations {

	/**
	 * @param args
	 */

	public SVNRepository getConnection() {

		String url = "https://opencollaborator.googlecode.com/svn/trunk/";
		String name = "chudasama.manvendrasinh@gmail.com";
		String password = "yX8tT2Yw2WU6";
		SVNRepository repository = null;

		DAVRepositoryFactory.setup();

		try {
			repository = SVNRepositoryFactory.create(SVNURL
					.parseURIDecoded(url));
			ISVNAuthenticationManager authManager = SVNWCUtil
					.createDefaultAuthenticationManager(name, password);
			repository.setAuthenticationManager(authManager);

			return repository;
		} catch (SVNException e) {
			e.printStackTrace();
			System.exit(1);
		}
		return null;
	}

	public static void listEntries(SVNRepository repository, String path)
			throws SVNException {
		Collection<SVNDirEntry> entries = repository.getDir(path, -1, null,
				(Collection<SVNDirEntry>) null);
		Iterator<SVNDirEntry> iterator = entries.iterator();
		while (iterator.hasNext()) {
			SVNDirEntry entry = (SVNDirEntry) iterator.next();
			System.out.println("/" + (path.equals("") ? "" : path + "/")
					+ entry.getName() + " ( author: '" + entry.getAuthor()
					+ "'; revision: " + entry.getRevision() + "; date: "
					+ entry.getDate() + ")");
			if (entry.getKind() == SVNNodeKind.DIR) {
				listEntries(repository, (path.equals("")) ? entry.getName()
						: path + "/" + entry.getName());
			}
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		BasicVersioningOperations bvo = new BasicVersioningOperations();
		SVNRepository svnrepo = bvo.getConnection();
		String url = "https://opencollaborator.googlecode.com/svn/trunk/";
		String dsturl = "https://opencollaborator.googlecode.com/svn/trunk/TestCode/test.txt";
		String name = "chudasama.manvendrasinh@gmail.com";
		String password = "yX8tT2Yw2WU6";
		ISVNAuthenticationManager authManager = SVNWCUtil
				.createDefaultAuthenticationManager(name, password);
		ISVNOptions options = SVNWCUtil.createDefaultOptions(true);
		SVNUpdateClient client = new SVNUpdateClient(authManager, options);
		File file = new File("C://Users//manav//Desktop//Test.txt");
		File commitFile = new File(
				"C://Users//manav//Desktop//Test//TestCode//test.txt");
		SVNCommitClient comClient = new SVNCommitClient(authManager, options);
		SVNDiffClient diffClient = new SVNDiffClient(authManager, options);

		try {

			SVNURL dstsvnurl = SVNURL.parseURIDecoded(dsturl);
			SVNURL svnurl = SVNURL.parseURIDecoded(url);
			FileOutputStream result = new FileOutputStream(file);
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			OutputStreamWriter ow = new OutputStreamWriter(result);
			// comClient.doImport(commitFile, dstsvnurl, "Test", true);
			diffClient.doDiff(commitFile, SVNRevision.WORKING, dstsvnurl,
					SVNRevision.HEAD, true, false, result);
			// client.doCheckout(svnurl, file, SVNRevision.HEAD,
			// SVNRevision.HEAD,
			// true, true);
			BasicVersioningOperations.listEntries(svnrepo, "");

		} catch (SVNException e) {
			// TODO Auto-generated catch block

			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
